﻿using System;
using Microsoft.SharePoint.Administration;
using System.Diagnostics;

namespace SharePointFarmInfoTimerJob
{
    /// <summary>
    /// SharePointFarmInfoTimerJob - Timer Job
    /// </summary>
    class SharePointFarmInfoTimerJob : SPJobDefinition
    {
        /// <summary>
        /// Timerjob Constructor
        /// </summary>
        public SharePointFarmInfoTimerJob()
            : base()
        {
            this.Title = Global.jobTitle;
        }

        /// <summary>
        /// Timerjob Constructor
        /// </summary>
        /// <param name="webApp">SPWebApplication</param>
        public SharePointFarmInfoTimerJob(SPWebApplication webApp)
            : base(Global.jobName, webApp, null, SPJobLockType.Job)
        {
            this.Title = Global.jobTitle;
        }

        /// <summary>
        /// Execute the timer job
        /// </summary>
        /// <param name="targetInstanceId">GUID</param>
        public override void Execute(Guid targetInstanceId)
        { 
            //Check for the key existence
            if (!Properties.ContainsKey(Global.SPSiteWebCollection) ||
                !Properties.ContainsKey(Global.SPList))
            {
                return;
            }

            //Retrive persisted object value
            string spWebName = Properties[Global.SPSiteWebCollection].ToString();
            string spListName = Properties[Global.SPList].ToString();

            //Treminate the operation if the required values are not present
            if (String.IsNullOrEmpty(spWebName) || String.IsNullOrEmpty(spListName))
            {
                return;
            }

            //Create/Update the SPFarm info
            SPFarmTopology farmInfo = new SPFarmTopology(spWebName, spListName);
        }
    }
}
